//
//  WallpaperItemView.swift
//  FreeWallpaperSwiftUI
//
//  Created by 张少康 on 2023/11/28.
//

import SwiftUI
import Kingfisher

struct WallpaperItemView: View {
    
    let processor = DownsamplingImageProcessor(size: CGSize(width: 320, height: 180))
        |> ResizingImageProcessor(referenceSize: CGSize(width: 320, height: 180), mode: .aspectFill)
    
    var model: WallpaperItemModel
    
    @State var loadSuccess = false
    
    @State var loadFailure = false
    
    @State var isAnimating: Bool = false
        
    var body: some View {
        ZStack {
            
            ProgressView() {
                Text("图片加载中").foregroundStyle(.gray)
            }
            .progressViewStyle(CircularProgressViewStyle())
            
            ZStack (alignment: .bottom) {
                KFImage(URL(string: "https://cn.bing.com" + (model.url ?? "")))
                    .setProcessor(processor)
                    .resizable()
                    .fade(duration: 0.25)
                    .onProgress { receivedSize, totalSize in
                    }
                    .onSuccess { result in 
                        
                    }
                    .onFailure { error in
                        
                    }
                    .aspectRatio(16/9, contentMode: .fit)
                    .clipped()
                    
                VStack (alignment: .leading){
                    HStack {
                        Text(model.title ?? "").font(.body).foregroundStyle(.white)
                        Spacer()
                    }
                    Text(model.copyright ?? "").font(.footnote).foregroundStyle(.white)
                        .padding(.bottom,12)
                }.padding(.horizontal)
                .background(
                    LinearGradient(colors: [.clear,.black], startPoint: UnitPoint(x: 0, y: 0), endPoint: UnitPoint(x: 0, y: 0.6))
                )

            }
        }.onAppear {
            isAnimating = true
        }
    }
}


#Preview {
    WallpaperItemView(model:  WallpaperItemModel(url: "/th?id=OHR.BradgateFallow_ZH-CN1852334581_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",title: "冬日私语",copyright: "阿西尼博因山省级公园，加拿大 (© matt macpherson/500px/Getty Images)"))
}
